package com.demo1221.listener;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
//@WebListener("/demo1221/mydemo01Listerner.do")
public class MyAttributeListerner implements ServletContextAttributeListener, HttpSessionAttributeListener, ServletRequestAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        System.out.println("ServletContext 添加属性：" + event.getName() + " = " + event.getValue());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        System.out.println("ServletContext 移除属性：" + event.getName() + " = " + event.getValue());
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        System.out.println("ServletContext 替换前属性：" + event.getName() + " = " + event.getValue());
        System.out.println("ServletContext 替换后属性：" + event.getName() + " = " + event.getServletContext().getAttribute(event.getName()));
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequest 添加属性：" + srae.getName() + " = " + srae.getValue());
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequest 移除属性：" + srae.getName() + " = " + srae.getValue());
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        System.out.println("ServletRequest 替换前属性：" + srae.getName() + " = " + srae.getValue());
        System.out.println("ServletRequest 替换后属性：" + srae.getName() + " = " + srae.getServletRequest().getAttribute(srae.getName()));
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("HttpSession 添加属性：" + event.getName() + " = " + event.getValue());
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        System.out.println("HttpSession 移除属性：" + event.getName() + " = " + event.getValue());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        System.out.println("HttpSession 替换前属性：" + event.getName() + " = " + event.getValue());
        System.out.println("HttpSession 替换后属性：" + event.getName() + " = " + event.getSession().getAttribute(event.getName()));
    }
}
